﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) [INCLUDE APACHE 2.0 AS AN OPTION.]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ppscd;
using ppscd.Configuration;
using System.Reflection;

namespace ppscdconsole
{
    class Program
    {
        static Program()
        {
            AppDomain.CurrentDomain.AssemblyResolve += AssemblyLoader.AssemblyResolve;
        }

        static void Main(string[] args)
        {            
            if (!File.Exists("ppcd.config"))
            {
                HandleError(new FileNotFoundException("Can't find ppcd.config"));
            }

            Console.WriteLine("Destination server is " + PerformancePointSettings.Current.DestinationServer);

            try
            {

             //TODO - refactor .settings file for testability
                new ContentDeployer().Deploy(PerformancePointSettings.Current.WorkspaceFile);
                Console.WriteLine("Deploy successfully finished");
                Console.ReadLine();
            }
            catch (Exception e)
            {
                 //what is the correct pattern here for consuming generic APIs and a console application wrapper?
                HandleError(e);
            }
        }

        static void HandleError(Exception e)
        {
            Console.WriteLine();
            Console.WriteLine("ERROR: " + e.Message);
            Console.WriteLine();
            
            if (e.InnerException != null)
            {
                Console.WriteLine(e.InnerException);
            }

            Console.WriteLine(e.StackTrace);
            Console.ReadLine();
            Environment.Exit(1);
        }
    }
}
